package com.ccx.coachsite.ui.softmgr;

import java.util.HashMap;

import android.content.Context;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.downloadmgr.DownloadTool;
import com.ccx.coachsite.model.SoftShareMessage;
import com.ccx.coachsite.ui.CommonAppView;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.util.OpenShareTool;

public class SoftMgrTopicWebView extends CommonAppView {

	private Context mContext;
	
	public CommonAppView parentView;
	
	private WebView webContent;
	private TextView login_top_title;
	private ImageButton btn_back;
	private String topicName,topicID,topicURL;
	private Button btnShare;
	
	private ProgressBar webProgressBar;
	private View llOnLoadingView;
	private View llOnDetailView;
	
	public SoftMgrTopicWebView(Context paramContext, HashMap<String, Object> initParaMap) {
		super(paramContext);
		
		mContext = paramContext;
		
		addView(R.layout.soft_mgr_soft_topic_webview);

		//专题列表显示返回导航
		topicName = (String)initParaMap.get( "TopicName" );
		topicID = (String)initParaMap.get( "TopicID" );
		topicURL = (String)initParaMap.get( "TopicURL" );
		
		llOnLoadingView = findViewById(R.id.ll_on_loading);
		llOnDetailView =  findViewById(R.id.ll_on_detail);
		llOnLoadingView.setVisibility(GONE);
		
		//llOnLoadingView.setVisibility(VISIBLE);
		//llOnDetailView.setVisibility(GONE);
		
		webProgressBar = (ProgressBar) findViewById(R.id.toturial_web_progress_bar);
		
		webContent = (WebView) findViewById(R.id.tutorial_detail_content);
		webContent.getSettings().setJavaScriptEnabled(true);
		webContent.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				webProgressBar.setProgress(progress);
				if (progress >= 100){
					webProgressBar.setVisibility(View.GONE);
					
					//llOnLoadingView.setVisibility(GONE);
					//llOnDetailView.setVisibility(VISIBLE);
				}
			}
		});
		webContent.loadUrl(topicURL);
		
		btnShare = (Button) findViewById(R.id.tutorial_button_share);
		btnShare.setOnClickListener(btnShareOnClickListener);
		btnShare.setText(OpenShareTool.getShareDesc(mContext));
		
		login_top_title = (TextView)findViewById(R.id.login_top_title);
	  	login_top_title.setText(topicName);
	  	login_top_title.setSelected(true);
	  	
	  	btn_back=(ImageButton)findViewById(R.id.login_btn_return);
	  	btn_back.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				if ( parentView != null )
					parentView.flushView();
			}
		});
	}
	
	OnClickListener btnShareOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (!LoginHelper.requireLogin(mContext))
				return;

			OpenShareTool.showOperDialog(mContext,topicName, "", topicID+ "", SoftShareMessage.TYPE_TOPIC_SHARE_TYPE,
					"专题", DownloadTool.SOFT_FLAG_DEFALUT_VALUE);
		}
	};
}
